#include "height_view.h"
#include <sstream>

HeightView::HeightView(Rectangle viewport,GLuint axisHandle,HeightModel& model)
	: View(viewport),model(model){
	dlhandle = axisHandle;
}

HeightView::~HeightView(){
}

void HeightView::setEnv(){
	glViewport ((GLint) viewport.x1,(GLint) viewport.y1, (GLsizei) viewport.width(), (GLsizei) viewport.height());
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();

	gluOrtho2D(0.0f,1.0f,0.0f,1.0f);
	//gluOrtho2D(-0.10, 1.05, -0.10, 1.05);
}

void HeightView::display(){
	setEnv();

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt (0, 0, 0.5, 0, 0, 0, 0, 1, 0);
	glCallList(dlhandle);


	glDisable(GL_LIGHTING);
	// Contorno del viewport
	glColor3f(0.5f,0.5f,0.5f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(0.0f,0.0f,0.0f);
		glVertex3f(0.0f,0.99f,0.0f);
		glVertex3f(0.99f,0.99f,0.0f);
		glVertex3f(0.99f,0.0f,0.0f);
	glEnd();

	// Line de referencia z=0;
	glPushMatrix();
		glTranslatef(0.0f,model.getGroundRef(),0.0f);
		glColor3f(0.0f,1.0f,1.0f);
		glBegin(GL_LINES);
			glVertex3f(0.0f,0.0f,0.0f);
			glVertex3f(1.0f,0.0f,0.0f);
		glEnd();

		glRasterPos3f(0.01, -0.04, 0.0);
		glutBitmapString(GLUT_BITMAP_HELVETICA_12,(unsigned char*)"z=0");

	glPopMatrix();

	CurveModel& curve = dynamic_cast<CurveModel&>(model);
	CurveModel::Points::const_iterator it = curve.pointsBegin();
	glColor3f(1.0f,1.0f,1.0f);
	glPointSize(3);
	glBegin(GL_LINE_STRIP);
		for(;it!=curve.pointsEnd();++it){
			glVertex2f(it->x1,it->x2);
		}
	glEnd();


	CurveModel::Points::const_iterator it2 = curve.controlPointBegin();
	glColor3f(0.0f,0.0f,1.0f);
	glPointSize(4);
	glBegin(GL_POINTS);
		for(;it2!=curve.controlPointEnd();++it2){
			glVertex2f(it2->x1,it2->x2);
		}
	glEnd();
	glPointSize(1);
	glEnable(GL_LIGHTING);
}

void HeightView::update(Observer* observer){
}
